package org.zachary.prosocialbehaviorservice.interfaces;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.zachary.prosocialbehaviorservice.infrastructure.utils.UUIdUtil;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/activity")
public class UploadController {
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://localhost:9000")
                    .credentials("admin","password123")
                    .build();

            String objectName = "images/" + UUIdUtil.createUUId();

            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket("activity")
                            .object(objectName)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );

            return "http://localhost:9000" + "/" + "activity" + "/" + objectName;
        } catch (Exception e) {
            log.error("上传失败", e);
            return "500";
        }
    }

}

